package com.design.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.design.blog.dao.UserinfoMapper;
import com.design.blog.model.Userinfo;
import com.design.blog.model.vo.PageNationParams;
import com.design.blog.model.vo.PageResult;
import com.design.blog.model.vo.UserinfoVO;
import com.design.blog.service.IUserinfoService;
import com.design.blog.utils.JWTUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @author hxg
 * @date 2022-02-28
 *  服务实现类
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements IUserinfoService {
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public Userinfo getUserInfo(String token) {
        Map<String, String> tokenMap = JWTUtils.checkToken(token);
        String userId = tokenMap.get("userId");
        return (Userinfo)redisTemplate.opsForValue().get(userId);
    }

    @Override
    public PageResult<Userinfo> getAllUseInfo(PageNationParams params) {
        Page<Userinfo> page = new Page<>(params.getCurrentPage(),params.getPageSize());
        LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(params.getQueryString())){
            queryWrapper.like(Userinfo::getAccount,params.getQueryString());
        }
        Page<Userinfo> userPage = userinfoMapper.selectPage(page, queryWrapper);

        PageResult<Userinfo> pageResult = new PageResult<>();
        pageResult.setList(userPage.getRecords());
        pageResult.setTotal(userPage.getTotal());
        return pageResult;
    }
}
